package com.hxy.org.controller;

import com.hxy.basic.query.util.AjaxResult;
import com.hxy.basic.query.util.PageList;
import com.hxy.org.domain.Department;
import com.hxy.org.query.DepartmentQuery;
import com.hxy.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hxy
 * 部门接口, controller控制层
 *  @Controller+@ResponseBody两个注解的结合
 *  swagger注解,为了让前端开发能够看懂
 */
@RestController
@RequestMapping("/department")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询所有
     * localhost:8080/department
     * @return List
     *
     */
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "") // swagger注解,为了让前端开发能够看懂
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    /**
     * 查询一个
     * @param id
     * @return
     * @GetMapping 本质上是 localhost:8080/department/id = localhost:8080/department"/{id}"
     * {}区分参数
     * 通过注解@PathVariable获取URL地址中的数据指定为("id")赋值给Long id
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一个对象",notes = "传入id")
    public Department findOne(@PathVariable("id") Long id) {
        return departmentService.findOne(id);
    }

    /**
     * 分页查询
     * 一般获取会用GetMapping 但是分页查询的参数过多. 就使用了PostMapping
     * 可以通过@RequestBody获取请求体中的DepartmentQuery数据
     * @param departmentQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "传入分页参数")
    public PageList<Department> queryList(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryPage(departmentQuery);
    }


    /**
     * 删除
     * 使用注解@DeleteMapping 获取参数与上面'查询一个'相同
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "传入id")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            // 调用删除方法
            departmentService.delete(id);
            // 默认Success为true,msg操作成功
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程,失败需要赋值Success与msg
            return AjaxResult.me().setSuccess(false).setMsg("删除失败," + e.getMessage());
        }
    }


    /**
     * 批量删除删除
     * 使用注解@DeleteMapping 获取参数与上面'查询一个'相同
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "需要传递删除的id")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("批量删除失败" + e.getMessage());
        }
    }

    /**
     * 修改与添加
     * 使用@PutMapping注解
     * 参数获取方法与上面'分页查询'相同
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加和修改",notes = "传入id就是修改,不传id就是添加")
    public AjaxResult saveOrUpdate(@RequestBody Department department) {
        try {
            // 没有id就是添加
            if(department.getId() == null){
                departmentService.save(department);
            } else {
                // 有就是修改
                departmentService.update(department);
            }
            // 默认Success为true,msg操作成功
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程,失败需要赋值Success与msg
            return AjaxResult.me().setSuccess(false).setMsg("操作失败," + e.getMessage());
        }
    }

    /**
     * 加载部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "加载部门树接口",notes = "")
    public List<Department> getDeptTree(){
        return departmentService.getDeptTree();
    }

}
